package com.cy.common.interceptor;

import com.cy.common.Exception.ServiceException;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;

//jdk1.8 以后接口可以拥有默认方法
public class TimeAccessInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LocalTime localTime=LocalTime.now();//JDK8 中获取当前时间
       int hour= localTime.getHour();
        System.out.println("hour==="+hour);
        if(hour<8||hour>22)
            throw new ServiceException("请在访问时间内访问");
        return true;//true放行，false拦截
    }
}
